<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Hello World</title>
  <script src="https://unpkg.com/vue"></script>

</head>

<body>
  <div id="root"></div>
  <script>
    /* 
      1. 通过事件实现通信
        - this.$emit()
        - @绑定事件
      2. 事件名命名规范
        - 小写短横
      3. 携带参数
      4. emits属性
      - 声明组件向外触发的事件，便于梳理/维护
      - 检验函数
      5. v-model双向数据绑定实现原理
      - input 通过绑定数据和事件实现
    */
    const counter = {
      props: ['count'],
      template: `<div>
                  {{count}}
                  <button @click="handleClick">自增</button>
                </div>`,
      methods: {
        handleClick() {
          this.$emit('add-count', 2)
        }
      }
    }
    const app = Vue.createApp({
      components: {
        counter
      },
      data() {
        return {
          count: 0
        }
      },
      template: `<div>
        <counter @add-count="handleAdd" :count="count"/>
      </div>`,
      methods: {
        handleAdd(val) {
          this.count += val
        }
      }
    })

    const vm = app.mount('#root');
  </script>
</body>

</html>